from django.db import models

# Create your models here.


# 基类
class BaseModel(models.Model):
    create_date = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_date = models.DateTimeField(auto_now=True, verbose_name="修改时间", null=True, blank=True)
    is_delete = models.BooleanField(default=False, verbose_name="是否删除")

    class Meta:
        abstract = True


# 用户表
class UserModel(BaseModel):
    username = models.CharField(max_length=50, verbose_name="账号")
    password = models.CharField(max_length=256, verbose_name="密码")
    school_id = models.BigIntegerField(verbose_name="学校id",  blank=True, null=True)
    grade_id = models.BigIntegerField(verbose_name="班级id", blank=True, null=True)
    position_id = models.BigIntegerField(verbose_name='职位',  blank=True, null=True)

    class Meta:
        db_table = 'user_tb'
        verbose_name = "用户表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.username


# 个人详细信息表
class UserDetailedModel(BaseModel):
    user_id = models.BigIntegerField(verbose_name="所属用户")
    account = models.CharField(max_length=30, verbose_name="姓名", null=True, blank=True)
    mobile = models.CharField(max_length=11, default="", verbose_name="手机号", blank=True, null=True)
    age = models.IntegerField(verbose_name="年龄", null=True, blank=True)
    sex = models.CharField(verbose_name="性别", max_length=10, default="", null=True, blank=True)
    avatar_url = models.CharField(max_length=256, verbose_name="头像", default="", null=True, blank=True)
    email = models.CharField(max_length=50, verbose_name="邮箱", default="", null=True, blank=True)

    class Meta:
        db_table = "user_deta_tb"
        verbose_name = "用户详情"
        verbose_name_plural = verbose_name
    def __str__(self):
        return f"用户：{self.user_id}"


# 学校表
class SchoolModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name="名称")
    desc = models.CharField(max_length=100, verbose_name="简介", null=True, blank=True,default="")
    user_id = models.BigIntegerField(verbose_name="负责人")

    class Meta:
        db_table = 'school_tb'
        verbose_name = "学校表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 班级表
class GradeMode(BaseModel):
    name = models.CharField(max_length=30, verbose_name="名称", null=True, blank=True)
    desc = models.CharField(max_length=50, verbose_name="简介", default="", null=True, blank=True)
    school_id = models.BigIntegerField(verbose_name='所属学校', null=True, blank=True)

    class Meta:
        db_table = 'grade_tb'
        verbose_name = "班级表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


# 教师班级中间表
class TeacherGradeModel(BaseModel):
    user_id = models.BigIntegerField(verbose_name="教师id")
    grade_id = models.BigIntegerField(verbose_name="班级id")
    class Meta:
        db_table = 'teacher_grade_tb'
        verbose_name = "教师班级中间表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return f"教师{self.user_id},班级{self.grade_id}"


# 角色表
class RoleModel(BaseModel):
    name = models.CharField(verbose_name="角色名称", max_length=30)
    desc = models.CharField(max_length=50, verbose_name="简介", default="", null=True, blank=True)

    class Meta:
        db_table = "role_tb"
        verbose_name = "角色表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 权限表
class PermissionModel(BaseModel):
    name = models.CharField(max_length=20, verbose_name='权限名称')
    desc = models.CharField(max_length=50, verbose_name="简介", default="", null=True, blank=True)

    class Meta:
        db_table = 'permission_tb'
        verbose_name = "权限表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name


# 角色权限中间表
class PositionModel(BaseModel):
    role_id = models.BigIntegerField(verbose_name="角色id", null=True, blank=True)
    permission_id = models.BigIntegerField(verbose_name="权限id", null=True, blank=True)

    class Meta:
        db_table = "position_tb"
        verbose_name = "角色权限中间表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return f"角色{self.role_id}，权限{self.permission_id}"


# 登录日志
class Operate_LogModel(BaseModel):
    name = models.CharField(verbose_name="操作信息", max_length=64)
    staff_id = models.BigIntegerField(verbose_name="登录用户")
    path = models.CharField(verbose_name="操作路径", max_length=256)
    ip = models.CharField(verbose_name="登录ip", max_length=15)
    address = models.CharField(verbose_name="地点", max_length=256)
    facility = models.CharField(verbose_name="设备", max_length=256)
    class Meta():
        db_table="operate_log_tb"
        verbose_name = "操作日志表"
        verbose_name_plural = verbose_name
    def __str__(self):
        return f"员工id{self.staff_id}，路径{self.path}"


# 问候语表
class GreetingsModel(BaseModel):
    content = models.CharField('问候内容', max_length=128)
    start_time = models.IntegerField(verbose_name='开始时间')
    end_time = models.IntegerField(verbose_name='结束时间')

    class Meta:
        db_table = 'greetings_tb'
        verbose_name = '问候语表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


# 消息表
class NewsModel(BaseModel):
    user_id = models.BigIntegerField("发起人id")
    take_id = models.BigIntegerField("接收人id")
    # 0未读 1已读
    status = models.IntegerField("读取状态", default=0)
    content = models.CharField("内容", max_length=256)

    class Meta:
        db_table = 'news_tb'
        verbose_name = "消息表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"发起人{self.user_id}, 接收人{self.take_id}"